package com.tul.exercise.base.swing.components;

import com.tul.exercise.base.swing.SwingConsole;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Description 《java编程思想》第22章 示例16：
 * <p>
 * 单选框
 **/
public class RadioButtons extends JFrame {
    private JTextField text = new JTextField(15);

    private ButtonGroups bg = new ButtonGroups();

    private JPanel jp = new JPanel();


    private JRadioButton rb1 = new JRadioButton("one", false);
    private JRadioButton rb2 = new JRadioButton("two", false);
    private JRadioButton rb3 = new JRadioButton("three", false);

    private ActionListener al = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String str = ((JRadioButton) e.getSource()).getText();
            //System.out.println(str);
            //text.setEditable(true);
            text.setText("Radio button " + str);
            text.setToolTipText("Radio button " + ((JRadioButton) e.getSource()).getText());

        }
    };

    public RadioButtons() {
        rb1.addActionListener(al);
        rb2.addActionListener(al);
        rb3.addActionListener(al);

        bg.add(rb1);
        bg.add(rb2);
        bg.add(rb3);

        text.setEditable(false);

        setLayout(new FlowLayout());
        add(text);
        //add(bg);
        add(rb1);
        add(rb2);
        add(rb3);


       /* jp.setBorder(new TitledBorder("Radio Checkboxes"));
        add(jp);
        jp.add(rb1);
        jp.add(rb2);
        jp.add(rb3);*/

    }

    public static void main(String[] args) {
        SwingConsole.run(new RadioButtons(), 200, 125);
    }
}
